package com.zjt.service.product;

import com.zjt.dao.product.IKnowledgeDao;
import com.zjt.dao.product.IProductCategoryDao;
import com.zjt.dao.product.IProductInfoDao;
import com.zjt.entity.product.Knowledge;
import com.zjt.entity.product.ProductCategory;
import com.zjt.entity.product.ProductInfo;
import com.zjt.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-29
 * Time: AM9:39
 * 防伪知识接口实现
 * To change this template use File | Settings | File Templates.
 */
@Component
public class KnowledgeService implements IKnowledgeService {
    private static Logger logger = LoggerFactory.getLogger(CodeService.class);
    private IKnowledgeDao knowledgeDao;
    private IProductCategoryDao productCategoryDao;
    private IProductInfoDao productInfoDao;

    @Autowired
    public void setProductInfoDao(IProductInfoDao productInfoDao) {
        this.productInfoDao = productInfoDao;
    }

    @Autowired
    public void setProductCategoryDao(IProductCategoryDao productCategoryDao) {
        this.productCategoryDao = productCategoryDao;
    }

    @Autowired
    public void setKnowledgeDao(IKnowledgeDao knowledgeDao) {
        this.knowledgeDao = knowledgeDao;
    }

    @Override
    public boolean saveKnowledge(long categoryId, String title, String content) {
        Knowledge knowledge = new Knowledge();
        String categoryIdRel = "";
        try
        {

            categoryIdRel = String.valueOf(categoryId)+",";
            knowledge.setCategoryIdRef(categoryIdRel);
            knowledge.setTitle(title);
            knowledge.setContent(content);
            knowledge.setCreateDate(DateUtil.getCurrentDate());
            knowledge.setUpdateDate(DateUtil.getCurrentDate());
            knowledge.setEnableFlag("Y");
            this.knowledgeDao.saveAndFlush(knowledge);
            return  true;
        }
        catch (Exception e){
            return false;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Knowledge> getKnowledgeListByCategoryId(long categoryId) {
        List<Long> paramList = new ArrayList<Long>();
        if (categoryId>0)
        {
            paramList.add(categoryId);
            return this.knowledgeDao.findByCategoryIdRefIn(paramList);  //To change body of implemented methods use File | Settings | File Templates.
        } else return null;
    }

    @Override
    public List<Knowledge> getKnowledgeListByBarCode(String barCode) {
        List<Knowledge> list = null;
        ProductInfo productInfo = this.productInfoDao.findByCodeValueOrGtin(barCode,barCode);
        if (productInfo!=null)
        {
            List<Long> paramList = new ArrayList<Long>();
            ProductCategory productCategory = this.productCategoryDao.findByProductId(productInfo.getId());
            if (productCategory!=null)
            {
                paramList.add(productCategory.getCategoryId());
                list = this.knowledgeDao.findByCategoryIdRefIn(paramList);
            }
        }
        return list;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Knowledge> getKnowledgeListByProductId(long productId) {
        List<Knowledge> list = null;
        ProductCategory productCategory = this.productCategoryDao.findByProductId(productId);
            if (productCategory!=null)
            {
                List<Long> paramList = new ArrayList<Long>();
                paramList.add(productCategory.getCategoryId());
                list = this.knowledgeDao.findByCategoryIdRefIn(paramList);

            }
        return list;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean refCategoryKnowledeg(long categoryId, long knowledgeId) {
        Knowledge knowledge = null;
        try
        {
            knowledge = this.knowledgeDao.findOne(knowledgeId);
            if (knowledge!=null)
            {
                String categoryIdRef = knowledge.getCategoryIdRef();
                categoryIdRef = categoryIdRef + String.valueOf(categoryId) +  ",";
                knowledge.setCategoryIdRef(categoryIdRef);
                this.knowledgeDao.saveAndFlush(knowledge);
            }
        }catch (Exception e)
        {

        }
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
